<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      title="检测结果录入"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="closeDialog"
      :close="closeDialog"
      :append-to-body="true"
    >
      <el-form ref="formData" :model="formData" status-icon :rules="formRules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="委托单号" :label-width="formLabelWidth">
              {{ formData.orderNum }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="委托科室" :label-width="formLabelWidth">
              {{ formData.department }}s
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="委托时间" :label-width="formLabelWidth">
              {{ formData.entrustDateTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划检测时间" :label-width="formLabelWidth">
              {{ formData.planDetectionDate }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="方法标准" :label-width="formLabelWidth">
              {{ formData.methodStandard }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方法标准名称" :label-width="formLabelWidth">
              {{ formData.methodStandardName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="检测人" :label-width="formLabelWidth">
              <el-input v-model="formData.resultDetectionPerson" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        ref="table"
        :data="formData.entrustTaskSampleInfoResultDtos"
        :span-method="objectSpanMethod"
        element-loading-text="Loading"
        fit
        height="300px"
        :cell-style="{ background: '#ffffff' }"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">{{ scope.row.serialNo }}</template>
        </el-table-column>
        <el-table-column label="样品编号">
          <template slot-scope="scope">{{ scope.row.sampleNum }}</template>
        </el-table-column>
        <el-table-column v-if="systemType == 'cdc'" label="受检单位/地址">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <el-table-column label="样品类别名称">
          <template slot-scope="scope">{{
            scope.row.sampleCategoryName
          }}</template>
        </el-table-column>
        <el-table-column label="样品名称">
          <template slot-scope="scope">{{ scope.row.sampleName }}</template>
        </el-table-column>
        <el-table-column label="检测项目">
          <template slot-scope="scope">{{ scope.row.detectionItem }}</template>
        </el-table-column>
        <el-table-column label="项目分项">
          <template slot-scope="scope">{{ scope.row.minuteItem }}</template>
        </el-table-column>

        <el-table-column label="单位符号">
          <template slot-scope="scope">{{ scope.row.unitSymbol }}</template>
        </el-table-column>
        <el-table-column label="检测结果" width="130px">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.result"
              filterable
              :disabled="updateState"
              allow-create
              default-first-option
              clearable
              placeholder="检测结果"
              @change="qualified(scope.$index)"
            >
              <el-option
                v-for="item in scope.row.sysBusinessDetectionLimits"
                :key="item.id"
                :label="item.detectionLimit"
                :value="item.detectionLimit"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="判定符号">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.decisionSymbol"
              :disabled="updateState"
              @change="qualified(scope.$index)"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="判定数值" width="130px">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.number"
              :disabled="updateState"
              @change="qualified(scope.$index)"
            ></el-input>
          </template>
        </el-table-column>
        <!-- <el-table-column label="质量指标">
          <template slot-scope="scope">
            {{ scope.row.qualityIndex }}
          </template>
        </el-table-column>-->
        <el-table-column label="是否合格">
          <template slot-scope="scope">{{ scope.row.resultStatus }}</template>
        </el-table-column>
        <el-table-column
          v-if="!updateState && systemType == 'thirdparty'"
          fixed="right"
          label="操作"
        >
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link type="primary" @click="selUnitELN(scope.$index)"
                >引用</el-link
              >
              <!-- <el-link type="primary" @click="cancelQuoteELN(scope.$index)"
                >取消引用</el-link
              >-->
              <!-- <el-link
                type="primary"
                @click="selectQuote(scope.row.experimentTemplateRecordId)"
              >
                查看
              </el-link>-->
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit(12)">确 定</el-button>
        <el-button type="primary" @click="onSubmit(11)">暂 存</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  entrustTaskResultList,
  // entrustTaskResultEntryList,
  entrustTaskSampleInfoResult,
  EntrustTaskSampleInfoResultAdd,
} from "./api/EntrustTaskManage/EntrustTask";
import { formatTime } from "@/utils/time";
export default {
  name: "AuditEntrustTaskDataResult",
  props: {
    dialogFormVisible: {
      type: Boolean, // 布尔
      default: false, // 必填
    },
    data: {
      type: Object, // 数组 // 对象或数组默认值必须从一个工厂函数获取
      default: () => { },
    },
    fromSource: {
      type: String,
      default: "",
    },
    dataGroup: {
      type: String,
      default: "0",
    },
    entrustOrderId: {
      type: String,
      default: "",
    },
  },

  data() {
    return {
      updateState: "",
      systemType: "",
      tableDatas: [],
      // 表单验证
      userArrary: [],
      list: [],
      formRules: {
        detectionPersonId: [{ required: true, message: "检测人不能为空" }],
        planDetectionDate: [
          { required: true, message: "计划检测时间不能为空" },
        ],
      },
      formLabelWidth: "100px",
      loading: false,
      formData: {
        id: "",
        entrustTaskStatus: 0,
        orderNum: "",
        department: "",
        entrustDateTime: "",
        planDetectionDate: "",
        methodStandard: "",
        methodStandardName: "",
        resultDetectionPerson: "",
        resultDetectionPersonId: "",
        entrustTaskSampleInfoResultDtos: [],
      },
      pageInfo: {
        pageIndex: 1,
        pageSize: 10,
      },
    };
  },
  watch: {
    formData(val) {
      this.doLayout();
    }
  },
  created() {
    var obj = {};
    obj.id = this.entrustOrderId;
    entrustTaskResultList(obj, this.pageInfo)
      .then((res) => {
        var dataFrom = res.data.dataList[0];
        this.formData.entrustTaskNum = dataFrom.entrustTaskNum;
        this.formData.entrustOrderNumList = dataFrom.entrustOrderNumList;
        this.formData.detectionItemName = dataFrom.detectionItemName;
        this.formData.sampleInfoName = dataFrom.sampleInfoName;
        this.formData.sampleInfoNum = dataFrom.sampleInfoNum;
        this.formData.planDetectionDate = dataFrom.planDetectionDate;
        this.formData.remark = dataFrom.remark;
        this.getList(dataFrom);
      })
      .catch((res) => { });
  },
  methods: {
    /* 重新渲染table组件 */
    doLayout() {
      const that = this
      this.$nextTick(() => {
        that.$refs.table.doLayout()
      })
    },
    // 是否合格
    qualified(index) {
      var row = this.formData.entrustTaskSampleInfoResultDtos[index];
      this.formData.entrustTaskSampleInfoResultDtos[index].qualityIndex =
        row.decisionSymbol + row.number;
      var symbol = row.decisionSymbol;
      var num = row.number;
      var numReg = /^(\-|\+)?\d+(\.\d+)?$/;
      var numRe = new RegExp(numReg);

      this.formData.entrustTaskSampleInfoResultDtos[index].rresultStatus = "";
      var state = null;
      if (!numRe.test(Number(num)) || !numRe.test(Number(row.result))) {
        if (num === row.result) {
          state = true;
        } else if (symbol === "∈") {
          state = false;
          if (row.result !== null && row.result !== "") {
            if (numRe.test(Number(row.result))) {
              var arr = num.split("-");
              if (numRe.test(Number(arr[0])) && numRe.test(Number(arr[1]))) {
                if (Number(arr[0]) > Number(arr[1])) {
                  state =
                    Number(arr[0]) >= Number(row.result) &&
                    Number(row.result) >= Number(arr[1]);
                } else if (Number(arr[0]) < Number(arr[1])) {
                  state =
                    Number(arr[0]) <= Number(row.result) &&
                    Number(row.result) <= Number(arr[1]);
                }
              }
            }
          }
        } else {
          state = false;
          // 截取检测结果第一个字符
          var resultSymbol = String(row.result).substring(0, 1);
          // 结果
          var result = String(row.result).substring(
            1,
            String(row.result).length
          );
          if (numRe.test(result)) {
            if (resultSymbol === ">" || resultSymbol === "≥") {
              state = false;
              if (
                (symbol === "≥" && Number(result) >= Number(num)) ||
                (symbol === ">" && Number(result) > Number(num))
              ) {
                state = true;
              }
            } else if (resultSymbol === "<" || resultSymbol === "≤") {
              state = false;
              if (
                (symbol === "≤" && Number(result) <= Number(num)) ||
                (symbol === "<" && Number(result) < Number(num))
              ) {
                state = true;
              }
            } else {
              state = false;
            }
          } else {
            state = false;
            // 判断是否为 未检出和无
            if (row.result === "未检出" || row.result === "无") {
              state = true;
            } else {
              state = false;
            }
          }
        }
      } else {
        state = this.resultJudgment(symbol, numRe, num, row.result);
      }
      if (
        this.formData.entrustTaskSampleInfoResultDtos[index].number !== null &&
        this.formData.entrustTaskSampleInfoResultDtos[index].number !== ""
      ) {
        if (state) {
          this.formData.entrustTaskSampleInfoResultDtos[index].resultStatus =
            "合格";
        } else if (!state && state != null) {
          this.formData.entrustTaskSampleInfoResultDtos[index].resultStatus =
            "不合格";
        } else {
          this.formData.entrustTaskSampleInfoResultDtos[index].resultStatus =
            "";
        }
      } else {
        this.formData.entrustTaskSampleInfoResultDtos[index].resultStatus = "";
      }
    },
    // 结果判断
    // symbol判定符号
    // numRe正则
    // num判定数值
    // result结果
    resultJudgment(symbol, numRe, num, result) {
      var state = null;
      if (symbol === "≥") {
        if (result !== null && result !== "") {
          if (numRe.test(Number(num)) && numRe.test(Number(result))) {
            state = Number(result) >= Number(num);
          }
        }
      } else if (symbol === "≤") {
        if (result !== null && result !== "") {
          if (numRe.test(Number(num)) && numRe.test(Number(result))) {
            state = Number(result) <= Number(num);
          }
        }
      } else if (symbol === "∈") {
        if (result !== null && result !== "") {
          if (numRe.test(Number(result))) {
            var arr = num.split("-");
            if (numRe.test(Number(arr[0])) && numRe.test(Number(arr[1]))) {
              if (Number(arr[0]) > Number(arr[1])) {
                state =
                  Number(arr[0]) >= Number(result) &&
                  Number(result) >= Number(arr[1]);
              } else if (Number(arr[0]) < Number(arr[1])) {
                state =
                  Number(arr[0]) <= Number(result) &&
                  Number(result) <= Number(arr[1]);
              }
            }
          }
        }
      } else if (symbol === ">") {
        if (result !== null && result !== "") {
          if (numRe.test(Number(num)) && numRe.test(Number(result))) {
            state = Number(result) > Number(num);
          }
        }
      } else if (symbol === "<") {
        if (result !== null && result !== "") {
          if (numRe.test(Number(num)) && numRe.test(Number(result))) {
            state = Number(result) < Number(num);
          }
        }
      } else {
        if (num === result) {
          state = true;
        } else {
          state = false;
        }
      }
      return state;
    },
    formatTime,
    currentChange(val) {
      this.pagination.pageIndex = val;
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
    },
    closeDialog() {
      // 动画结束后在清空表单 防止页面闪烁
      this.$refs.formData.resetFields();
      this.$emit("update:dialogFormVisible", false);
      this.$emit("close");
    },
    onSubmit(state) {
      var xuhao = "";
      for (
        var i = 0;
        i < this.formData.entrustTaskSampleInfoResultDtos.length;
        i++
      ) {
        if (
          this.formData.entrustTaskSampleInfoResultDtos[i].result === "" ||
          this.formData.entrustTaskSampleInfoResultDtos[i].result === null
        ) {
          this.$message.warning("录入结果不能为空!");
          return;
        }

        if (state === 12) {
          if (
            this.formData.entrustTaskSampleInfoResultDtos[i].resultStatus ===
            "不合格"
          ) {
            if (xuhao === "") {
              xuhao += (i + 1).toString();
            } else {
              xuhao += (i + 1).toString() + ",";
            }
          }
        }
      }

      if (xuhao !== "") {
        var msg = "序号" + xuhao + "检测项目结果不合格，请确认是否继续提交?";
        this.$confirm(msg, "提示", {
          confirmButtonText: "取消",
          cancelButtonText: "继续提交",
          type: "warning",
          center: true,
        })
          .then(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          })
          .catch(() => {
            this.UpdateResultBitchFun(state);
          });
      } else {
        this.UpdateResultBitchFun(state);
      }
    },
    UpdateResultBitchFun(status) {
      this.formData.entrustTaskStatus = status;
      EntrustTaskSampleInfoResultAdd(this.formData).then((res) => {
        this.$emit("getList");
        this.closeDialog();
      });
    },
    getList(data) {
      this.list = [];
      var obj = {};
      obj.entrustTaskIdList = data.id;

      entrustTaskSampleInfoResult(obj).then((res) => {
        this.formData = res.data;
        if (
          res.data.resultDetectionPerson !== null &&
          res.data.resultDetectionPerson !== ""
        ) {
          this.formData.resultDetectionPerson = res.data.resultDetectionPerson;
        } else {
          this.formData.resultDetectionPerson = this.$store.getters.name;
          this.formData.resultDetectionPersonId =
            this.$store.getters.currentUserId;
        }
      });
    },
    // 选择列表
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
  },
};
</script>
